package com.jinlia.show.modules.wxapp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinlia.show.common.core.exception.MyRuntimeException;
import com.jinlia.show.common.core.utils.UserInfoUtils;
import com.jinlia.show.modules.base.constant.DeliveryTypeEnum;
import com.jinlia.show.modules.base.model.form.OrderForm;
import com.jinlia.show.modules.base.model.po.DeliveryOrderPO;
import com.jinlia.show.modules.base.model.query.OrderQuery;
import com.jinlia.show.modules.base.model.po.OrderPO;
import com.jinlia.show.modules.base.system.entity.DeliveryOrder;
import com.jinlia.show.modules.base.system.service.DeliveryOrderMapperService;
import com.jinlia.show.modules.base.system.service.OrderMapperService;
import com.jinlia.show.modules.base.wxapp.entity.PayOrder;
import com.jinlia.show.modules.base.wxapp.entity.UserAddress;
import com.jinlia.show.modules.wxapp.model.form.NewOrderForm;
import com.jinlia.show.modules.wxapp.model.vo.OrderVO;
import com.jinlia.show.modules.wxapp.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

@RestController
@RequiredArgsConstructor
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderMapperService orderMapperService;

    @PostMapping("page")
    public ResponseEntity<IPage<OrderPO>> page(@RequestBody OrderQuery orderQuery) {
        orderQuery.setUserId(UserInfoUtils.getUserId());
        orderQuery.setNeedDeliveryOrder(Boolean.FALSE);
        return ResponseEntity.ok(orderMapperService.pageList(orderQuery));
    }
    @PostMapping("/new")
    public PayOrder newOrder(@RequestBody NewOrderForm orderForm) {
        orderForm.setUserId(UserInfoUtils.getUserId());
        if (DeliveryTypeEnum.OTHER_PICT.getValue().equals(orderForm.getDeliveryType())) {//配送
            //校验订单参数
            DeliveryOrder deliveryOrder = orderForm.getDeliveryOrder();
            if (Objects.isNull(deliveryOrder)) {
                throw new MyRuntimeException("请选择填写配送地址");
            }
        }
        if (orderForm.getCompanyId() == null) {
            throw new MyRuntimeException("请选择填门店");
        }
        orderForm.setAppId(UserInfoUtils.getAppId());
        return orderService.newOrder(orderForm);
    }
    /**
     * 通过购物车下单(未支付)
     */
    @PostMapping("/add/by/shoppingCar")
    public PayOrder addByShoppingCar(@RequestBody OrderForm orderForm) {
        orderForm.setUserId(UserInfoUtils.getUserId());
        if (DeliveryTypeEnum.OTHER_PICT.getValue().equals(orderForm.getDeliveryType())) {//配送
            //校验订单参数
            DeliveryOrderPO deliveryOrder = orderForm.getDeliveryOrder();
            if (Objects.isNull(deliveryOrder)) {
                throw new MyRuntimeException("请选择填写配送地址");
            }
        }
        if (orderForm.getCompanyId() == null) {
            throw new MyRuntimeException("请选择填门店");
        }
        orderForm.setAppId(UserInfoUtils.getAppId());
        return orderService.addByShoppingCar(orderForm);
    }

    /**
     * 更新订单
     */
    @PostMapping("/update/{id}")
    public Boolean updateOrder(@PathVariable("id") Long orderId,@RequestBody OrderPO orderPO) {
        orderPO.setId(orderId);
        orderService.updateOrder(orderPO);
        return Boolean.TRUE;
    }
    /**
     * 取消订单
     *
     * @param orderPO
     * @return
     */
    @PostMapping("/del")
    public ResponseEntity<Boolean> page(@RequestBody OrderPO orderPO) {
        return ResponseEntity.ok(orderService.cancel(orderPO.getId()));
    }

    /**
     * 确认收货
     *
     * @param id
     * @return
     */
    @GetMapping("/receive/{id}")
    public ResponseEntity<Boolean> confirmReceive(@PathVariable("id") Long id) {
        return ResponseEntity.ok(orderService.confirmReceive(id));
    }

    /**
     * 订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ResponseEntity<OrderVO> detailOrder(@PathVariable("id") Long id) {
        return ResponseEntity.ok(orderService.getOrderVO(id));
    }

    //我的订单看板
    @GetMapping("/board")
    public ResponseEntity<Map<String, String>> myOrderBoard() {
        return ResponseEntity.ok(orderService.myOrderBoard());
    }
}
